package com.hdsx.jxcsxm.tjbb.controller;

import com.hdsx.jxcsxm.tjbb.bean.Bkzh;
import com.hdsx.jxcsxm.tjbb.bean.Excel_list;
import com.hdsx.jxcsxm.tjbb.bean.Gtzjjhlr;
import com.hdsx.jxcsxm.tjbb.bean.GtzjjhlrMx;
import com.hdsx.jxcsxm.tjbb.server.SbzjGtzjServer;
import com.hdsx.jxcsxm.utile.EasyUIPage;
import com.hdsx.jxcsxm.utile.ExcelReader;
import com.hdsx.jxcsxm.utile.ExcelReaderUtils;
import com.hdsx.jxcsxm.utile.JsonUtils;
import com.hdsx.jxcsxm.utile.MyUtil;
import com.hdsx.jxcsxm.utile.ResponseUtils;
import com.hdsx.webutil.struts.BaseActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller
public class SbzjgtzjController extends BaseActionSupport implements ModelDriven<Excel_list> {

    @Resource(name = "sbzjGtzjServerImpl")

    private SbzjGtzjServer sbzjGtzjServer;

//    private Gtzjjhlr gtzjjhlr = new Gtzjjhlr();
//    private GtzjjhlrMx gtzjjhlrMx = new GtzjjhlrMx();
//    private File fileupload;
//    private String fileuploadFileName;
//    private int page = 1;
//    private int rows = 10;
    private Gtzjjhlr gtzjjhlr = new Gtzjjhlr();
    private GtzjjhlrMx gtzjjhlrMx = new GtzjjhlrMx();
    private File fileupload;
    private String fileuploadFileName;
    private int page = 1;
    private int rows = 10;
    private Excel_list elist = new Excel_list();

    public Gtzjjhlr getGtzjjhlr() {
        return gtzjjhlr;
    }

    public void setGtzjjhlr(Gtzjjhlr gtzjjhlr) {
        this.gtzjjhlr = gtzjjhlr;
    }

    public GtzjjhlrMx getGtzjjhlrMx() {
        return gtzjjhlrMx;
    }

    public void setGtzjjhlrMx(GtzjjhlrMx gtzjjhlrMx) {
        this.gtzjjhlrMx = gtzjjhlrMx;
    }

    public File getFileupload() {
        return fileupload;
    }

    public void setFileupload(File fileupload) {
        this.fileupload = fileupload;
    }

    public String getFileuploadFileName() {
        return fileuploadFileName;
    }

    public void setFileuploadFileName(String fileuploadFileName) {
        this.fileuploadFileName = fileuploadFileName;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Excel_list getElist() {
        return elist;
    }

    public void setElist(Excel_list elist) {
        this.elist = elist;
    }

    @Override
    public Excel_list getModel() {
        return null;
    }

    public void checkgtzjNameCf() {
        boolean bl = sbzjGtzjServer.checkgtzjNameCf(gtzjjhlr);
        ResponseUtils.write(getresponse(), bl + "");
    }

    public void autoGtzjxmmc() throws Exception {
        List<Gtzjjhlr> l=new ArrayList<Gtzjjhlr>();
        l=sbzjGtzjServer.autoGtzjxmmc(gtzjjhlr.getXmmc().trim());

        if(l.size()==0) {
            // Xmjbxx x=new Xmjbxx();
            // x.setXmmc(xmjbxx.getXmmc());
            // l.add(x);
            Gtzjjhlr GtzjjhlrA = new Gtzjjhlr();
            GtzjjhlrA.setXmmc(GtzjjhlrA.getXmmc());
            l.add(GtzjjhlrA);
        }
        JsonUtils.write(l,	getresponse().getWriter());
    }

    public void selectsbzjGtzjList() {
        gtzjjhlr.setPage(page);
        gtzjjhlr.setRows(rows);
        gtzjjhlr.setXmnf(MyUtil.getQueryTJ(gtzjjhlr.getXmnf(), "xmnf"));

        List<Gtzjjhlr> list = sbzjGtzjServer.selectsbzjGtzjList(gtzjjhlr);
        int count = sbzjGtzjServer.selectsbzjGtzjListCount(gtzjjhlr);
        EasyUIPage<Gtzjjhlr> e = new EasyUIPage<Gtzjjhlr>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    //项目详细信息查询
    public void queryGtzjlist() {
        //System.out.println(gtzjjhlr);
        //System.out.println(gtzjjhlrMx);

        if (gtzjjhlrMx.getPage() == 0) {
            gtzjjhlrMx.setPage(page);
        }
        if (gtzjjhlrMx.getRows() == 0) {
            gtzjjhlrMx.setRows(rows);
        }
        List<GtzjjhlrMx> list = sbzjGtzjServer.queryGtzjlist(gtzjjhlrMx);
        int count = sbzjGtzjServer.queryGtzjlistCount(gtzjjhlrMx);
        EasyUIPage<GtzjjhlrMx> e = new EasyUIPage<GtzjjhlrMx>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    //删除项目（包含所属子项目）
    public void delGtzjjhlr() {
        boolean bl = sbzjGtzjServer.delGtzjjhlr(gtzjjhlr);
        ResponseUtils.write(getresponse(), bl + "");
    }
    //删除项目明细（单独）
    public void delGtzjjhlrMx() {
        boolean bl = sbzjGtzjServer.delGtzjjhlrMx(gtzjjhlrMx);
        ResponseUtils.write(getresponse(), bl + "");
    }

    //
    public void sumyeBygtzjBJID() {
        try {
            System.out.println("传入数据"+gtzjjhlrMx);
            GtzjjhlrMx bl = sbzjGtzjServer.sumyeBygtzjBJID(gtzjjhlrMx);
            System.out.println("b1======="+bl);
            JsonUtils.write(bl, getresponse().getWriter());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void updateSbzjgtzjjhlrMx() {
        boolean bl = sbzjGtzjServer.updateSbzjgtzjjhlrMx(gtzjjhlrMx);
        ResponseUtils.write(getresponse(), bl + "");
    }

    public void updateSbzjgtzjjhlr(){
        boolean bl = sbzjGtzjServer.updateSbzjgtzjjhlr(gtzjjhlr);
        ResponseUtils.write(getresponse(), bl + "");
    }

    public void insertSbzjGtzjjhlr(){
        String fileType=fileuploadFileName.substring(fileuploadFileName.lastIndexOf(".")+1, fileuploadFileName.length());
        HttpServletResponse response = getresponse();

        try {
            if(!"xls".equals(fileType)&&!"xlsx".equals(fileType)){
                response.getWriter().print(fileuploadFileName+"不是系统导出的excel模版文件");
                return ;
            }
            boolean isExcel2003 = true;
            if (fileuploadFileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            response.setCharacterEncoding("utf-8");
            FileInputStream fs = new FileInputStream(this.fileupload);
            List<Map>[] dataMapArray;
            //System.out.println("isExcel2003============="+isExcel2003);
            if (isExcel2003)
                dataMapArray = ExcelReaderUtils.readExcelContent(0, 24, fs, Object.class);
            else
                dataMapArray = ExcelReaderUtils.readExcelContent2(0, 24, fs, Object.class);
            //dataMapArray = ExcelReader.readExcelContent(0,20,fs,Object.class);
            //System.out.println(dataMapArray[0]);
            List<Map> data = ExcelReader.removeBlankRow(dataMapArray[0]);
            List<Map> bt =new ArrayList<Map>();
            List<Map> gdxm =new ArrayList<Map>();
            List<Map> ncglxm =new ArrayList<Map>();
            List<Map> nr =new ArrayList<Map>();
            List<Map> list =new ArrayList<Map>();
            if(data.size()>=1) {
                for (Map d :
                        data) {
                    if (!d.get("0").equals("") && d.get("0") != null && d.get("0").toString().replaceAll("\\s*", "").contains("计划录入")) {
                        bt.add(d);
                    }//设区市	新建	改建	路面改造	养护大中修	灾毁恢复	危桥改造	安防工程	灾害防治	养护工程	示范路
                    if ("设区市".equals(d.get("0").toString().replaceAll("\\s*", ""))&& "新建".equals(d.get("1").toString().replaceAll("\\s*", "")) &&
                            "改建".equals(d.get("2").toString().replaceAll("\\s*", "")) &&  "路面改造".equals(d.get("3").toString().replaceAll("\\s*", "")) &&
                            "灾毁恢复".equals(d.get("5").toString().replaceAll("\\s*", "")) &&  "安防工程".equals(d.get("7").toString().replaceAll("\\s*", "")) &&
                            "养护工程".equals(d.get("9").toString().replaceAll("\\s*", "")) &&  "服务区".equals(d.get("11").toString().replaceAll("\\s*", "")) &&
                            "道班".equals(d.get("13").toString().replaceAll("\\s*", "")) &&  "平交道口".equals(d.get("15").toString().replaceAll("\\s*", "")) &&
                            "连续长陡下坡".equals(d.get("17").toString().replaceAll("\\s*", ""))&& "隧道提质升级".equals(d.get("19").toString().replaceAll("\\s*", "")) &&
                            "养护建设项目".equals(d.get("22").toString().replaceAll("\\s*", ""))) {
                        gdxm.add(d);
                    }
                    if ("设区市".equals(d.get("0").toString().replaceAll("\\s*", ""))&& "危桥改造".equals(d.get("1").toString().replaceAll("\\s*", "")) &&
                            "安防工程".equals(d.get("2").toString().replaceAll("\\s*", "")) &&  "农村公路建设".equals(d.get("3").toString().replaceAll("\\s*", "")) &&
                            "通自然村".equals(d.get("5").toString().replaceAll("\\s*", "")) &&  "民族村".equals(d.get("7").toString().replaceAll("\\s*", "")) &&
                            "组组通水泥路".equals(d.get("9").toString().replaceAll("\\s*", "")) &&  "民族村县道升级改造".equals(d.get("11").toString().replaceAll("\\s*", "")) &&
                            "养护建设项目".equals(d.get("13").toString().replaceAll("\\s*", ""))) {
                        ncglxm.add(d);
                    }
                    if (!d.get("0").toString().replaceAll("\\s*", "").contains("计划录入") &&
                            !"设区市".equals(d.get("0").toString().replaceAll("\\s*", ""))){
                        nr.add(d);
                    }
                }
                //System.out.println("bt=========="+bt);
                //System.out.println("gdxm=========="+gdxm);
                //System.out.println("ncglxm=========="+ncglxm);
                //System.out.println("nr=========="+nr);

                String xmdjserach = gtzjjhlr.getXmdj();
                if ("国省道".equals(xmdjserach)){
                    if (!"国省道资金计划录入".equals(bt.get(0).get("0").toString().replaceAll("\\s*", ""))){
                        ResponseUtils.write(getresponse(), "表头不是国省道资金计划录入");
                        //response.getWriter().print(fileuploadFileName+"标题不正确");
                        return;
                    }else {
                        if (gdxm.size() > 0 && "设区市".equals(gdxm.get(0).get("0").toString().replaceAll("\\s*", ""))&&
                                "路面改造".equals(gdxm.get(0).get("3").toString().replaceAll("\\s*", ""))&&
                                "安防工程".equals(gdxm.get(0).get("7").toString().replaceAll("\\s*", ""))) {
                            //处理数据
                            for (int i = 0; i < nr.size(); i++) {
                                //Map<String, String> cgsjhlrMapmx1 = new HashMap<String, String>();
                                //项目类型  new2>new1?30:0
                                for(int j = 1; j < 23; j++) {
                                    Map<String, String> cgsjhlrMapmx = new HashMap<String, String>();
                                    //!nr.get(i).get(j).toString().replaceAll("\\s*", "").isEmpty()&&nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "").matches("^[-\\\\\\\\+]?([0-9]+\\\\\\\\.?)?[0-9]+$")
                                    if (!nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "").isEmpty()) {
                                        String replaceAll = nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "");
                                        //System.out.println("原始数据"+replaceAll);
                                        boolean matches = replaceAll.matches("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$");
                                        //System.out.println("正则表达式之后"+matches);
                                        if(matches) {
                                            cgsjhlrMapmx.put("xmlx", gdxm.get(0).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                            cgsjhlrMapmx.put("id", uuid);
                                            cgsjhlrMapmx.put("jhid", gtzjjhlr.getId());
                                            cgsjhlrMapmx.put("sqrq", gtzjjhlr.getSqrq());
                                            cgsjhlrMapmx.put("bcje", nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            cgsjhlrMapmx.put("zhjhje",nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            cgsjhlrMapmx.put("bz", gtzjjhlr.getBz());
                                            String xmdj = gtzjjhlr.getXmdj();
                                            String xmlx = nr.get(i).get("0").toString().replaceAll("\\s*", "");
                                            //System.out.println(nr.get(i).get("0").toString().replaceAll("\\s*", "").contains("鹰潭"));
                                            boolean contains = nr.get(i).get("0").toString().replaceAll("\\s*", "").contains("鹰潭");
                                            if (contains) {

                                                Bkzh autocgshmNcgl = sbzjGtzjServer.autocgshmNcgl(xmlx);
                                                if(autocgshmNcgl.getId().toString()!=null) {
                                                    //数据库查询sbzj_bkzh中得id//select * from SBZJ_BKZH where  dwmc like '吉安市'||'%'||'公路'||'%';
                                                    cgsjhlrMapmx.put("zhid", autocgshmNcgl.getId());
                                                }else {
                                                    response.getWriter().print(fileuploadFileName+"xmdj不能未空");
                                                }

                                            }else {
                                                if ("国省道".equals(xmdj)) {
                                                    Bkzh autocgshmGsd = sbzjGtzjServer.autocgshmGsd(xmlx);
                                                    //System.out.println("autocgshmGsd.getId().isEmpty()"+autocgshmGsd.getId());
                                                    //System.out.println("autocgshmGsd.getId().isEmpty()"+autocgshmGsd.getId().isEmpty());
                                                    if(!autocgshmGsd.getId().isEmpty()) {
                                                        //System.out.println("autocgshmGsd.getId().isEmpty()00000"+autocgshmGsd.getId());
                                                        //数据库查询sbzj_bkzh中得id//select * from SBZJ_BKZH where  dwmc like '吉安市'||'%'||'公路'||'%';
                                                        cgsjhlrMapmx.put("zhid", autocgshmGsd.getId());
                                                    }else {
                                                        ResponseUtils.write(getresponse(), "项目等级不能为空");
                                                        //response.getWriter().print(fileuploadFileName+"xmdj不能未空");
                                                    }

                                                }
                                            }
                                        }else {
                                            ResponseUtils.write(getresponse(), "填写内容不是数字格式");
                                            return;
                                        }
                                        list.add(cgsjhlrMapmx);
                                    }
                                }
//
                            }
                        }else {
                            ResponseUtils.write(getresponse(), "execl标题不正确");
                            //response.getWriter().print(fileuploadFileName+"标题不正确");
                            return;
                        }
                    }
                }
                if ("农村公路".equals(xmdjserach)){
                    if (!"农村公路资金计划录入".equals(bt.get(0).get("0").toString().replaceAll("\\s*", ""))){
                        ResponseUtils.write(getresponse(), "表头不是农村公路资金计划录入");
                        //response.getWriter().print(fileuploadFileName+"标题不正确");
                        return;
                    }else {
                        //设区市	危桥改造	安防工程	农村公路建设	养护工程	通自然村	psl贷款	民族村	旅游公路	组组通水泥路	民族村通自然村公路
                        // 民族村县道升级改造	美丽生态文明农村路	养护建设项目
                        if (ncglxm.size() > 0 && "设区市".equals(ncglxm.get(0).get("0").toString().replaceAll("\\s*", ""))&&
                                "农村公路建设".equals(ncglxm.get(0).get("3").toString().replaceAll("\\s*", ""))&&
                                "民族村".equals(ncglxm.get(0).get("7").toString().replaceAll("\\s*", "")) &&
                                "美丽生态文明农村路".equals(ncglxm.get(0).get("12").toString().replaceAll("\\s*", "")) ) {
                            //处理数据
                            for (int i = 0; i < nr.size(); i++) {
                                //Map<String, String> cgsjhlrMapmx1 = new HashMap<String, String>();
                                //项目类型  new2>new1?30:0
                                for(int j = 1; j <= 13; j++) {
                                    Map<String, String> cgsjhlrMapmx = new HashMap<String, String>();
                                    //!nr.get(i).get(j).toString().replaceAll("\\s*", "").isEmpty()&&nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "").matches("^[-\\\\\\\\+]?([0-9]+\\\\\\\\.?)?[0-9]+$")
                                    if (!nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "").isEmpty()) {
                                        String replaceAll = nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", "");
                                        //System.out.println("原始数据"+replaceAll);
                                        boolean matches = replaceAll.matches("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$");
                                        //System.out.println("正则表达式之后"+matches);
                                        if(matches) {
                                            cgsjhlrMapmx.put("xmlx", ncglxm.get(0).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                            cgsjhlrMapmx.put("id", uuid);
                                            cgsjhlrMapmx.put("jhid", gtzjjhlr.getId());
                                            cgsjhlrMapmx.put("sqrq", gtzjjhlr.getSqrq());
                                            cgsjhlrMapmx.put("bcje", nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            cgsjhlrMapmx.put("zhjhje",nr.get(i).get(String.valueOf(j)).toString().replaceAll("\\s*", ""));
                                            cgsjhlrMapmx.put("bz", gtzjjhlr.getBz());
                                            String xmdj = gtzjjhlr.getXmdj();
                                            String xmlx = nr.get(i).get("0").toString().replaceAll("\\s*", "");
                                            //System.out.println(nr.get(i).get("0").toString().replaceAll("\\s*", "").contains("鹰潭"));
                                            boolean contains = nr.get(i).get("0").toString().replaceAll("\\s*", "").contains("鹰潭");
                                            if (contains) {
                                                Bkzh autocgshmNcgl = sbzjGtzjServer.autocgshmNcgl(xmlx);
                                                if(autocgshmNcgl.getId().toString()!=null) {
                                                    //数据库查询sbzj_bkzh中得id//select * from SBZJ_BKZH where  dwmc like '吉安市'||'%'||'公路'||'%';
                                                    cgsjhlrMapmx.put("zhid", autocgshmNcgl.getId());
                                                }else {
                                                    response.getWriter().print(fileuploadFileName+"xmdj不能未空");
                                                }

                                            }else {
                                                if ("农村公路".equals(xmdj)) {
                                                    Bkzh autocgshmNcgl = sbzjGtzjServer.autocgshmNcgl(xmlx);
                                                    if(!autocgshmNcgl.getId().isEmpty()) {
                                                        //数据库查询sbzj_bkzh中得id//select * from SBZJ_BKZH where  dwmc like '吉安市'||'%'||'公路'||'%';
                                                        cgsjhlrMapmx.put("zhid", autocgshmNcgl.getId());
                                                    }else {
                                                        response.getWriter().print(fileuploadFileName+"xmdj不能未空");
                                                    }

                                                }
                                            }
                                        }else {
                                            ResponseUtils.write(getresponse(), "填写内容不是数字格式");
                                            return;
                                        }
                                        list.add(cgsjhlrMapmx);
                                    }
                                }
                            }
                        }else {
                            ResponseUtils.write(getresponse(), "execl标题不正确");
                            return;
                        }
                    }
                }
            }else {
                ResponseUtils.write(getresponse(), "execl表内容为空");
                return;
            }
            //System.out.println("listss-------------"+"list-------------"+list.size()+list);
            double total = 0;
            for (int k = 0; k < list.size(); k++) {
                total+=Double.parseDouble(String.valueOf(list.get(k).get("bcje")));
            }
            double totaljhje= Double.parseDouble(gtzjjhlr.getJhje());
            //System.out.println(total+"+++++++"+totaljhje);
            //System.out.println(total==totaljhje);
            if (total!=totaljhje) {
                ResponseUtils.write(getresponse(), bt.get(0).get("0").toString().replaceAll("\\s*", "")+"记划录入总金额不等于计划金额");
                return;
            }
            //tem.out.println("list-------------"+"list-------------"+list.size()+list);
            //将数据插入到数据库
            boolean b=sbzjGtzjServer.insertSbzjGtzjjhlr(gtzjjhlr,list);
            if(b)
                ResponseUtils.write(getresponse(), b + "");
            else
                ResponseUtils.write(getresponse(), "");
            //response.getWriter().print(fileuploadFileName+"导入失败");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public void sumyegtzj(){
        try {
            Gtzjjhlr bl = sbzjGtzjServer.selectsumyegtzj(gtzjjhlr);
            JsonUtils.write(bl, getresponse().getWriter());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }




}
